<script setup>
	import { reactive, ref } from 'vue';
	import { loginByMobileApi, verifyCodeApi } from '@/services/user';
	import { useUserStore } from '@/stores/user';

	const showCountdown = ref(false)
	const buttonText = ref('获取验证码')
	
	const userStore = useUserStore()
	const formRef = ref(null)
	
	// 发送短信验证码
	async function onTextButtonClick () {
		try {
			await formRef.value.validateField(['mobile'])
			showCountdown.value = true
			const { code, message, data } = await verifyCodeApi({
				mobile: formData.mobile,
				type: 'login'
			})
			
			// 检测接口是否调用成功
			if (code !== 10000) return uni.utils.toast(message)
			formData.code = data.code
			
			uni.utils.toast('验证码已发送，请查收!')
			showCountdown.value = true
		} catch (err) {
			console.log(err, 'err');
		}
	}
	
	function onCountdownTimeup () {
		buttonText.value = '重新获取验证码'
		showCountdown.value = false
	}
	
	const isAgree = ref(false)
	function onAgreeClick () {
		isAgree.value = !isAgree.value
	}
	
	const formData = reactive({
		mobile: '',
		code: '',
	})
	
	const formRules = {
		mobile: {
		  rules: [
			{ required: true, errorMessage: '请填写手机号码' },
			{ pattern: '^1\\d{10}$', errorMessage: '手机号码格式不正确' },
		  ],
		},
		code: {
		  rules: [
			{ required: true, errorMessage: '请输入验证码' },
			{ pattern: '^\\d{6}$', errorMessage: '验证码格式不正确' },
		  ],
		}
	}
	
	// 点击登录
	async function onFormSubmit () {
		try {
			
			// 判断是否同意协议
			if (!isAgree.value) return uni.utils.toast('请先同意协议')
			
			const formData = await formRef.value.validate()
			
			const {
				code, 
				message,
				data
			} = await loginByMobileApi(formData)
			
			if (code !== 10000) return uni.utils.toast(message)
			
			userStore.token = data.token
			
			// 临时跳转到首页面
			uni[userStore.openType]({
				url: userStore.recirectURL
			})
		} catch (err) {
			console.log(err, 'err');
		}
	}
	
</script>

<template>
  <uni-forms
    class="login-form"
    :rules="formRules"
    :model="formData"
    ref="formRef"
  >
    <uni-forms-item name="mobile">
      <uni-easyinput
        :input-border="false"
        :clearable="false"
        v-model="formData.mobile"
        placeholder="请输入手机号"
        placeholder-style="color: #C3C3C5"
      />
    </uni-forms-item>
    <uni-forms-item name="code">
      <uni-easyinput
        :input-border="false"
        :clearable="false"
        v-model="formData.code"
        placeholder="请输入验证码"
        placeholder-style="color: #C3C3C5"
      />
      <view v-if="showCountdown" class="text-button">
        <custom-countdown
          :showHour="false"
          :showMiniute="false"
          :showDay="false"
          :second="10"
          color="#16C2A3"
          @timeup="onCountdownTimeup"
        ></custom-countdown>
        秒后重新获取
      </view>
      <text v-else @click="onTextButtonClick" class="text-button">
        {{ buttonText }}
      </text>
    </uni-forms-item>

    <view class="agreement">
      <radio @click="onAgreeClick" :checked="isAgree" color="#16C2A3" />
      我已同意
      <text class="link">用户协议</text>
      及
      <text class="link">隐私协议</text>
    </view>

    <button @click="onFormSubmit" class="uni-button">登 录</button>
  </uni-forms>
</template>

<script>
  export default {
    options: {
      styleIsolation: 'shared',
    },
  }
</script>

<style lang="scss">
  @import './styles.scss';
</style>
